﻿using NCS.IConnect.Cloud.Core.Properties;
using System;
using System.Text.RegularExpressions;

namespace NCS.IConnect.Cloud.Core
{
    /// <summary>
    /// iConnect.Azure specific Guard for general validation.
    /// </summary>
    public class Guard3
    {
        private const string patternTableName = @"^[A-Za-z][A-Za-z0-9]{2,62}$";
        private const string patternKeyFieldValue = @"[/|\\|#|\?]";

        /// <summary>
        /// Ensures the valid table storage table names.
        /// <remarks>
        /// 1. Table names may contain only alphanumeric characters.
        /// 2. A table name may not begin with a numeric character.
        /// 3. Table names are case-insensitive.
        /// 4. Table names must be from 3 through 63 characters long.
        /// </remarks>
        /// </summary>
        /// <param name="tableName">Name of the table.</param>
        public static void EnsureValidTableNames(string tableName)
        {
            if (!Regex.IsMatch(tableName, patternTableName))
            {
                throw new ArgumentException(ResourceUtility.Format(Resources.ExceptionTableStorageTableNameInvalid, tableName));
            }
        }

        /// <summary>
        /// Ensures the valid partition key value or row key value.
        /// <remarks>
        /// The following characters are not allowed in values for the PartitionKey and RowKey properties:
        /// 1. The forward slash (/) character
        /// 2. The backslash (\) character
        /// 3. The number sign (#) character
        /// 4. The question mark (?) character
        /// </remarks>
        /// </summary>
        /// <param name="keyFieldValue">The key field value.</param>
        public static void EnsureValidKeyFieldValue(string keyFieldValue)
        {
            if (Regex.IsMatch(keyFieldValue, patternKeyFieldValue))
            {
                throw new ArgumentException(ResourceUtility.Format(Resources.ExceptionTableStorageKeyFieldsValueInvalid, keyFieldValue));
            }
        }
    }
}
